******************************************************************************************
* 5.1 FIGURE OF EVENT DISTRIBUTION
******************************************************************************************
clear
set more off
set mem 100m

use Data_HIPC_Events.dta, clear

replace hipc1 = 1 if Date==13327 /*6/27/1996*/
replace hipc1 = 1 if Date==13421  /*9/29/1996*/
replace hipc2 = 1 if Date==14413  /*6/18/1999*/
replace hipc2 = 1 if Date==14513  /*9/26/1999*/
replace mdri = 1 if Date==16625  /*7/8/2005*/
replace mdri = 1 if Date==16705  /*09/26/2005*/
replace mdri = 0 if Date==16806  /*1/5/2006*/

gen dsa_ago=0
gen dsa_gnq = 0
gen dsa_ken=0

reshape long dsa_ pre_ exec_ dec_ comp_ dsa_e_ pre_e_ exec_e_ dec_e_ prorep_e_ comp_e_ prodsa_e_, i(Date) j(cty) string

sort cty Date

egen check = rsum(hipc1 hipc2 mdri dsa_ pre_ exec_ dec_ comp_ dsa_e_ pre_e_ exec_e_ dec_e_ prorep_e_ comp_e_ prodsa_e_)
egen count = anymatch(hipc1 hipc2 mdri dsa_ pre_ exec_ dec_ comp_ dsa_e_ pre_e_ exec_e_ dec_e_ prorep_e_ comp_e_ prodsa_e_), values(1)


expandcl check if check==2, generate(expandid) cluster(check)

gen event=.
replace event = 1 if dec_==1
replace event = 2 if comp_==1 & check==1
replace event = 2 if comp_==1 & check==2 & expandid==1
replace event = 3 if dec_e_==1 & check==1
replace event = 3 if dec_e_==1 & check==2 & expandid==2
replace event = 4 if comp_e_==1


** BY COUNTRY

*Dropping countries where there is no ZAF investment
drop if cty=="bdi" | cty=="sle" | cty=="tcd" | cty=="gnb" | cty=="ner" | cty=="stp" | cty=="gmb" | cty=="ben" | cty=="bfa" | cty=="sen" | cty=="mrt"

gen rcomp_e_mdri = 0
replace rcomp_e_mdri = 1 if comp_e_==1 & Date>=16763
egen after = max(rcomp_e_mdri), by (cty)

gen dec_e_order=0
replace dec_e_order = Date if dec_e_==1
egen order1 = max(dec_e_order), by (cty)
egen ctyid1 = group(order1 cty)

gen comp_e_order=0
replace comp_e_order = Date if comp_e_==1
egen order2 = max(comp_e_order), by (cty)
egen ctyid2 = group(order2 order1 cty)

egen ctyid = group(cty)
replace event= 5 if Date==13300


*** ORDER BY dec_e
twoway (scatter ctyid1 Date if event==1, msize(small) mcolor(dknavy) msymbol(plus)) ///
(scatter ctyid1 Date if event==2, msize(small) mcolor(dknavy) msymbol(circle_hollow)) ///
(scatter ctyid1 Date if event==3, msize(small) mcolor(dknavy) msymbol(x)) ///
(scatter ctyid1 Date if event==4, msize(small) mcolor(dknavy) msymbol(circle)) ///
(scatter ctyid1 Date if event==5, msymbol(none)), ///
xlabel(13327"6\27\96" 13421"9\29\96" 14413"6\18\99" 14513"9\26\99" 16625"7\8\05" 16705"9\26\05" 13880"1\1\98" 14976"1\1\01" 15706"1\1\03" 17167"1\1\07", tlength(0.5) labsize(tiny) angle(stdarrow) alternate) ///
xline(13327 13421, lpattern(dot) lcolor(blue)) xline(14407 14513, lpattern(vshortdash) lcolor(green)) xline(16598 16763, lpattern(vshortdash) lcolor(purple)) ///
xscale(range(13000 17500)) legend(label(1 "Decision HIPC") label(2 "Completion HIPC") label(3 "Decision E-HIPC") label(4 "Completion E-HIPC")) ///
ylabel(, tlength(0.5) labsize(tiny) angle(horizontal) grid glpattern(vshortdash) glwidth(vvvthin) glcolor(navy)) ///
ylabel(1"Angola" 2 "Coted'Ivoire" 3"Equatorial Guinea" 4 "Kenya" 5"Uganda" 6"Tanzania" 7"Mozambique" ///
8 "Mali" 9"Cameroon" 10"Zambia" 11"Guinea" 12"Madagascar" ///
13"Malawi" 14"Rwanda" 15"Ethiopia" ///
16"Ghana" 17"Congo Dem. Rep." 18"Congo Rep.") scale(1.5) legend(off) xtitle("") ytitle("")

/***After plotting, manually correct x and y axes***
Overall:
  - Change mergin in region and inner region to zero
Plot region:
  - Change margin to small
Marker:
  - Change the size of circle_hollow to medium
  - Change the size of x to medium
  - Change the size of circle to medium-small
x axis:
  - Drop title
  - Rename labels nicely (use / /)
  - Add labels of some landmark dates if necessary
	(13880, 14976, 15706 and 17167 representing Jan 1 in 98, 01, 03 and 07)
  - Change text gap for these 3 ticks to 1.5, 0, 1.5 and 0, respectively
y axis:
  - Drop title
*/

gr save Figure_2, replace
